package com.tianlan.blog.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.tianlan.blog.entity.SysResource;
import com.tianlan.blog.entity.SysUser;
import com.tianlan.blog.service.SysResourceService;
import com.tianlan.common.annotation.auth.CurrentUser;
import com.tianlan.common.annotation.log.AroundLog;
import com.tianlan.common.base.Result;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

/**
 * <p>
 * 权限表 前端控制器
 * </p>
 *
 * @author tl-wxq
 * @since 2020-05-01
 */
@Api(value = "ResourceController|权限资源相关的前端控制器")
@RestController
@RequestMapping("/resource")
public class ResourceController {
	
	@Autowired
	private SysResourceService sysResourceService;
	
    @GetMapping(value = "/tree")
    @RequiresRoles("SYSADMIN")
    @ApiOperation(value = "查询权限资源树", notes = "树状展示权限资源信息")
    @ApiImplicitParam(paramType = "query", name = "parentId", value = "父级ID", required = false, dataType = "Integer")
    public Result<List<SysResource>> queryResourceTree(Integer parentId) {
        List<SysResource> treeList = sysResourceService.queryResourceByParentId(parentId);
        return new Result<List<SysResource>>().success().put(treeList);
    }
    
    @PostMapping("/create")
    @RequiresRoles("SYSADMIN")
    @ApiOperation(value = "添加权限资源")
    @AroundLog(name = "添加权限资源")
    public Result<SysResource> createResource(@RequestBody SysResource resource) {
        boolean result = sysResourceService.createResource(resource);
        if (result) {
            return new Result<SysResource>().success("添加成功").put(resource);
        } else {
            return new Result<SysResource>().error("添加失败，请重试");
        }
    }
    
    @PostMapping("/update")
    @RequiresRoles("SYSADMIN")
    @ApiOperation(value = "更新权限资源")
    @AroundLog(name = "更新权限资源")
    public Result<SysResource> updateResource(@RequestBody SysResource resource) {
        boolean result = sysResourceService.updateResource(resource);
        if (result) {
            return new Result<SysResource>().success("修改成功").put(resource);
        } else {
            return new Result<SysResource>().error("修改失败");
        }
    }
    
    @RequiresRoles("SYSADMIN")
    @PostMapping("/delete/{resId}")
    @ApiOperation(value = "删除权限资源")
    @AroundLog(name = "删除权限资源")
    @ApiImplicitParam(paramType = "path", name = "resId", value = "权限资源ID", required = true, dataType = "Integer")
    public Result<?> deleteResource(@PathVariable("resId") Integer resId) {
        boolean result = sysResourceService.deleteResource(resId);
        if (result) {
            return new Result<>().success("删除成功");
        } else {
            return new Result<>().error("删除失败");
        }
    }
    
    @PostMapping(value = "/key/check")
    @RequiresRoles("SYSADMIN")
    @ApiOperation(value = "校验ResourceKey是否存在", notes = "校验ResourceKey是否存在")
    public Result<Boolean> checkResourceKey(SysResource resource) {
        long count = sysResourceService.checkResourceKey(resource);
        if (count == 0){
            return new Result<Boolean>().success().put(true);
        } else{
            return new Result<Boolean>().success().put(false);
        }
    }
    
    /**
     * 获取登录后的菜单权限
     */
    @GetMapping("/menu")
    @RequiresAuthentication
    @ApiOperation(value = "登陆后获取个人权限资源")
    public Result<List<SysResource>> navMenu(@ApiIgnore @CurrentUser SysUser currentUser) {
        Integer userId = currentUser.getId();
        List<SysResource> resourceList = sysResourceService.queryResourceByUserId(userId);
        return new Result<List<SysResource>>().success().put(resourceList);
    }
}
